<?php

class TrackingController extends Core_Controller_Action {

    public function indexAction() {
        if ("mobile" == Core_MobileDetect::getCurrentDevice()) {
            $this->indexmobileAction();
            $this->render('indexmobile');
        } else {
            $filters = array();
            $filters['ref_no'] = $this->_getParam("ref_no");
            $filters['vessel'] = $this->_getParam("vessel");
            $filters['port_of_call'] = $this->_getParam("port_of_call");
            $filters['ata'] = $this->_getTimestampParam('ata');
            $filters['ats'] = $this->_getTimestampParam('ats');
            $this->view->filters = $filters;

            $gi_db = new Core_Model_DbTable_GeneralInformation();
            $page = $this->_getParam("page");
            $paginator_adapter = $gi_db->getPaginatorAdapter($filters);
            $paginator = new Zend_Paginator($paginator_adapter);
            $paginator->setItemCountPerPage(SHIP_PER_PAGE);
            $paginator->setCurrentPageNumber($page);
            $this->view->ships = $paginator;
            $this->view->paginator = $paginator;
            $port_db = new Core_Model_DbTable_Ports();
            $this->view->ports = $port_db->getAll();
        }
    }

    public function indexmobileAction() {
        $this->_helper->layout()->setLayout("layout_mobile");
        $filters = array();
        $filters['ref_no'] = $this->_getParam("ref_no");
        $filters['vessel'] = $this->_getParam("vessel");
        $filters['port_of_call'] = $this->_getParam("port_of_call");
        $filters['ata'] = $this->_getTimestampParam('ata');
        $filters['ats'] = $this->_getTimestampParam('ats');
        $this->view->filters = $filters;

        $gi_db = new Core_Model_DbTable_GeneralInformation();
        $paginator_adapter = $gi_db->getPaginatorAdapter($filters);
        if ($paginator_adapter == null) {
            $this->_handlePageNotFound();
        }
        $paginator = new Zend_Paginator($paginator_adapter);
        $this->view->ships = $paginator;
        $this->view->paginator = $paginator;
        $port_db = new Core_Model_DbTable_Ports();
        $this->view->ports = $port_db->getAll();
    }

    public function deleteServiceAction() {
        $this->setAjaxCall();

        $id = $this->_getParam("giid");
        if (!$id) {
            $this->_handlePageNotFound();
        }

        $gi_db = new Core_Model_DbTable_GeneralInformation();
        $gi = $gi_db->getById($id);
        if ( !$gi ) {
            $this->_handlePageNotFound();
        }

        $input_docs_db = new Core_Model_DbTable_InputDocs();
        $input_docs = $input_docs_db->getByGIID($id);
        
        foreach ( $input_docs as $input_doc) {
            unlink(UPLOAD_PATH . $input_doc['file']);
        }
        
        $cond = $input_docs_db->getAdapter()->quoteInto("gi_id = ?", $id);
        $input_docs_db->delete($cond);
        
        $input_photo_db = new Core_Model_DbTable_InputPhotos();
        $input_photos = $input_photo_db->getByGIID($id);
        
        foreach ($input_photos as $input_photo) {
            unlink(UPLOAD_PATH . $input_photo['file']);
        }
        
        $cond = $input_photo_db->getAdapter()->quoteInto("gi_id = ?", $id);
        $input_photo_db->delete($cond);
        
        if ( !empty($gi['vessel_on_arrival']) ) {
            unlink(UPLOAD_PATH . $gi['vessel_on_arrival']);
        }
        
        if ( !empty($gi['vessel_on_departure']) ) {
            unlink(UPLOAD_PATH . $gi['vessel_on_departure']);
        }
        
        $cond = $gi_db->getAdapter()->quoteInto("id = ?", $id);
        $gi_db->delete($cond);
        
        $this->_helper->redirector("index");
    }

}

?>
